package br.ucb.filtros;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;

/**
 * Servlet Filter implementation class FitroUser
 */
@WebFilter("/fitroUser")
public class FiltroUser implements Filter {
	

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
		HttpServletRequest req = (HttpServletRequest) request;
		// pass the request along the filter chain
		if(isSessao(req)) {  
          chain.doFilter(request, response);
		} else {
		  request.setAttribute("precisoLogar", "");
		  RequestDispatcher d = request.getRequestDispatcher("login.jsp");
		  d.forward(request, response);
		}
	}

	public boolean isSessao(HttpServletRequest request) {
		HttpSession session = request.getSession();
		if(session == null || session.getAttribute("usuario") == null || session.getAttribute("usuario") == "")
			return false;
		return true;
	}
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
